<?php
/**
 * Simple Curl wrapper
 * @package  nuo
 * @author   Nam Nguyen <nam.ngvan@gmail.com>
 * @version  1.0
 * @since    Apr 17, 2013
 */
class Nuo_Curl
{

	/**
	 * CURL_OPTION
	 * @access protected
	 * @var    array
	 */
	private $_curlOpts = array(
		CURLOPT_TIMEOUT => 5,
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_FOLLOWLOCATION => 2,
		CURLOPT_SSL_VERIFYHOST => 0,
		CURLOPT_SSL_VERIFYPEER => 0,
		CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
	);

	/**
	 * @access   public
	 * @param    string  $key
	 * @param    string  $value
	 * @return   Nuo_Curl
	 */
	public function setOption($key, $value)
	{
		$this -> _curlOpts[$key] = $value;
		return $this;
	}

	/**
	 * Get curl option of current instance
	 * @access  public
	 * @param   string $key
	 * @return  any
	 */
	public function getOption($key)
	{
		return isset($this -> _curlOpts[$key]) ? $this -> _curlOpts[$key] : NULL;
	}
	
	/**
	 * Send curl request
	 * @param   string  $url
	 * @param   string  $method Optional
	 * @param   array   $params Optional
	 * @param   array   $headers Optional
	 * @return  string
	 */
	public function send($url, $method = 'GET', $params = array(), $headers = array())
	{
		
		$ch = curl_init($url);

		curl_setopt_array($ch, $this -> _curlOpts);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$res = curl_exec($ch);

		if (curl_errno($ch))
		{
			throw new Exception(curl_error($ch));
		}

		return $res;
	}

}
